package com.wsj.web.action.homeSiteNews;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IHomeSiteNewsCommentServ;
import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.HtmlCreateObj;
import com.wsj.util.tool.StringUtils;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.HomeSiteNews.HomeSiteNewsCommentForm;
import com.wsj.web.vo.homeSite.HomeSiteNews;
import com.wsj.web.vo.homeSite.HomeSiteNewsComment;

public class NewsCommentAction extends BaseAction {

	
	/** 资讯接口 */
	private IHomeSiteNewsServ homeSiteNewsServImpl;
	
	/** 资讯评论接口 */
	private IHomeSiteNewsCommentServ homeSiteNewsCommentServ;
	
	//	html页面生成对象
	private HtmlCreateObj htmlCreateObj;
	
	
	
	//  评论没有修改，只能添加
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HomeSiteNewsCommentForm hsForm = (HomeSiteNewsCommentForm)form;
		HomeSiteNewsComment hs = new HomeSiteNewsComment();
		BeanUtils.copyProperties(hs, hsForm);
		//  资讯
		Long newsId = hsForm.getHomeSiteNewsId();		
		HomeSiteNews hsNews = this.homeSiteNewsServImpl.getHomeSiteNewsById(newsId);
		if(hsNews != null){
			request.setAttribute("news", hsNews);
		}
		//  获取评论人IP 
		hs.setIp(request.getRemoteAddr());		
		//  设置批评时间
		hs.setDeliverDatetime(new Date());
		this.homeSiteNewsCommentServ.saveOrUpdate(hs);
		return mapping.findForward("toList");
	}
	
	
	//  批量操作：设置删除
	public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer id = null;
		if(request.getParameter("id") != null){
			id = Integer.parseInt(request.getParameter("id"));
			HomeSiteNewsComment hs = (HomeSiteNewsComment)this.homeSiteNewsCommentServ.loadById(id);
			Short isDel = hs.getIsDel();
			if(isDel.equals(new Short("0"))){ 
				hs.setIsDel((short) 1);
			}else{
				hs.setIsDel((short) 0);
			}
			this.setPopMsg(request, "msg", "是否删除，设置成功！");
		}
		return mapping.findForward("toList");
	}
	
	//  批量操作：审核通过
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer[] ids = null;
		HomeSiteNewsCommentForm hsForm = (HomeSiteNewsCommentForm)form;
		
		//  设置是否审核的ID
		ids = hsForm.getCheckboxparam();
		if(ids != null  && ids.length>0){
			for(int i=0;i<ids.length;i++){
				Integer id = ids[i];
				HomeSiteNewsComment hs = (HomeSiteNewsComment)this.homeSiteNewsCommentServ.loadById(id);
				Short audite = hs.getAudite();
				if(audite.equals(new Short("0"))){ 
					hs.setAudite((short) 1);
				}else{
					hs.setAudite((short) 0);
				}
			}
			this.setPopMsg(request, "msg", "是否通过审核，批量设置成功！");
		}
		
		return mapping.findForward("toList");
	}
	
	//  显示
	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer id = null;
		if(request.getParameter("id") != null){
			id = Integer.parseInt(request.getParameter("id"));
			HomeSiteNewsComment hs = (HomeSiteNewsComment)this.homeSiteNewsCommentServ.loadById(id);
			if(hs != null){
				request.setAttribute("hsComment", hs);
			}
		}
		return mapping.findForward("toShow");
	}
	
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer page = 0;
		Integer pageSize = 20;
		Long nid = null;
		
		if(StringUtils.isNotNull(request.getParameter("nid"))){
			nid = Long.parseLong(request.getParameter("nid"));
		}
		
		if(StringUtils.isNotNull(request.getParameter("page"))){
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		this.loadList(nid, null, null, request, page, pageSize);
			
		return mapping.findForward("list");
	}

	
	@SuppressWarnings("unchecked")
	private void loadList(Long newsId,Short isDel,Short audite,HttpServletRequest request,Integer page, Integer pageSize){
		try {

			  PageResultSet pageResultSet = this.homeSiteNewsCommentServ.loadPageList(newsId, isDel, audite, page, pageSize);
			  
			  if(pageResultSet!=null)
			  {
			      //设置上一页
			      request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
			      //下一页
			      request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
			      //当前页
			      request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
			      
			      //总共页数
			      request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
			      
			      request.setAttribute(PageContent.page, page);
			      request.setAttribute(PageContent.pageSize, pageResultSet.getPageSize());
			      
			      //总记录数
			      request.setAttribute(PageContent.recordNum,pageResultSet.getRecordNum());
			      
			      //开始行码
			      request.setAttribute(PageContent.startRow,pageResultSet.getStartRow());
			      
			      //结束行码
			      request.setAttribute(PageContent.endRow,pageResultSet.getEndRow());			      			      
			      
			      List<HomeSiteNewsComment> li = pageResultSet.getResult();
			      request.setAttribute("HomeSiteNewsComment_list",li);
			      request.setAttribute("nid", newsId);
					HomeSiteNews hsNews = this.homeSiteNewsServImpl.getHomeSiteNewsById(newsId);
					if(hsNews != null){
						request.setAttribute("news", hsNews);
					}
			      
			  }
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
	}
	
	
	public IHomeSiteNewsServ getHomeSiteNewsServImpl() {
		return homeSiteNewsServImpl;
	}

	public void setHomeSiteNewsServImpl(IHomeSiteNewsServ homeSiteNewsServImpl) {
		this.homeSiteNewsServImpl = homeSiteNewsServImpl;
	}

	public HtmlCreateObj getHtmlCreateObj() {
		return htmlCreateObj;
	}

	public void setHtmlCreateObj(HtmlCreateObj htmlCreateObj) {
		this.htmlCreateObj = htmlCreateObj;
	}


	public IHomeSiteNewsCommentServ getHomeSiteNewsCommentServ() {
		return homeSiteNewsCommentServ;
	}


	public void setHomeSiteNewsCommentServ(
			IHomeSiteNewsCommentServ homeSiteNewsCommentServ) {
		this.homeSiteNewsCommentServ = homeSiteNewsCommentServ;
	}

}
